# datetime: time模块升级版
"""
datetime模块:
    time    时间
    date    日期  (data 数据)
    datetime    日期时间
    timedelta   时间差

"""
import datetime
import time

print(datetime.time.hour)  # <attribute 'hour' of 'datetime.time' objects>
print(time.localtime().tm_hour)  # 16

d = datetime.date(2019, 12, 1)
print(d.day)  # 1
print(datetime.date.day)  # <attribute 'day' of 'datetime.date' objects>
print(datetime.date.ctime(d))  # Sun Dec  1 00:00:00 2019

# datetime  timedelta
print(datetime.date.today())  # 2021-12-08

# 时间差
timedel = datetime.timedelta(hours=2)
print(timedel)  # 2:00:00

now = datetime.datetime.now()  # 当前的日期和时间
print(now)  # 2021-12-08 16:46:41.588709
result = now - timedel
print(result)  # 2021-12-08 14:46:41.588709

# 应用
# 缓存：数据库(redis) 作为缓存    redis.set(key, value, 时间差)  会话：session
